from ..ess import typeof

__all__ = [
    "isBasePlus",
    "basePlus",
    "addmethod",
    "methodDict",
]


def isBasePlus(it):
    return isinstance(it, basePlus)


methodDict: dict = {
    "__hash__": lambda self: hash(self.contain),
    "__int__": lambda self: int(self.contain),
    "__str__": lambda self: str(self.contain),
    "__bool__": lambda self: bool(self.contain),
    "__float__": lambda self: float(self.contain),
    "__bytes__": lambda self: bytes(self.contain),
    "__complex__": lambda self: complex(self.contain),
    "__radd__": lambda self, other: self.same(other) + self,
    "__iadd__": lambda self, other: self.same(self + other),
    "__rsub__": lambda self, other: -self + other,
    "__isub__": lambda self, other: self.same(self - other),
    "__rmul__": lambda self, other: self.same(other) * self,
    "__imul__": lambda self, other: self.same(self * other),
    "__rpow__": lambda self, other: self.same(other) ** self,
    "__ipow__": lambda self, other: self.same(self ** other),
    "__ror__": lambda self, other: self.same(other) | self,
    "__ior__": lambda self, other: self.same(self | other),
    "__rand__": lambda self, other: self.same(other) & self,
    "__iand__": lambda self, other: self.same(self & other),
    "__rxor__": lambda self, other: self.same(other) ^ self,
    "__ixor__": lambda self, other: self.same(self ^ other),
    "__rmod__": lambda self, other: self.same(other) % self,
    "__imod__": lambda self, other: self.same(self % other),
    "__ge__": lambda self, other: not self < other,
    "__ne__": lambda self, other: self != other,
    "__le__": lambda self, other: self < other or self == other,
    "__gt__": lambda self, other: self >= other and self != other,
    "__rmatmul__": lambda self, other: self.same(other) @ self,
    "__imatmul__": lambda self, other: self.same(self @ other),
    "__rtruediv__": lambda self, other: self.same(other) / self,
    "__itruediv__": lambda self, other: self.same(self / other),
    "__rlshift__": lambda self, other: self.same(other) << self,
    "__ilshift__": lambda self, other: self.same(self << other),
    "__rrshift__": lambda self, other: self.same(other) >> self,
    "__irshift__": lambda self, other: self.same(self >> other),
    "__rfloordiv__": lambda self, other: self.same(other) // self,
    "__ifloordiv__": lambda self, other: self.same(self // other),
    "__divmod__": lambda self, other: (self // other, self % other),
    "__rdivmod__": lambda self, other: (other // self, other % self),
    "__repr__": lambda self: f"{typeof(self)}({repr(self.contain)})",
    "__contains__": lambda self, item: any(i == item for i in self),
    "__format__": lambda self, __format_spec: format(self.contain, __format_spec),
}


def addmethod(methodDict: dict = methodDict):
    return lambda cls: type(cls.__name__, (cls,), methodDict)


@addmethod(methodDict)
class basePlus:
    """Plus基类"""

    def __init__(self, value=None):
        self.contain = value

    def same(self, *other):
        return self.__class__(*other)

    def copy(self):
        return self.same(self.contain)

    def subtype(self) -> type:
        return type(self.contain)
